
import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter)

// 重写方法
const originPush = VueRouter.prototype.push  // 先将之前的存储到一个变量当中
const originReplace = VueRouter.prototype.replace // 同上

// f
VueRouter.prototype.push = function(location,okCallBack,errCallBack){
  if(okCallBack === undefined && errCallBack === undefined){
    originPush.call(this,location).catch(()=>{}) 
  }else {
    originPush.call(this,location,okCallBack,errCallBack)
  }
}

VueRouter.prototype.replace = function(location,okCallBack,errCallBack){
  if(okCallBack === undefined && errCallBack === undefined){
    originReplace.call(this,location).catch(()=>{})  
  }else {
    originReplace.call(this,location,okCallBack,errCallBack)
  }
}

import Home from '@/viws/Home'
import Enroll from '@/viws/Enroll'
import Login from '@/viws/Login'
import Notfound from '@/viws/Notfound'
import Search from '@/viws/Search'
import Detail from '@/viws/Detail'
import Comshopcart from '@/viws/AddCartSuccess'
import Shopcart from '@/viws/ShopCart'
import Trade from '@/viws/Trade'

let router
export default router= new VueRouter ({
    mode:'history',
    routes:[
       {
        path:'/',
        name:'home',
        component:Home,
        meta:{
          isshownav:true,
          aaa:11111
        }
       },
       {
        path:'/enroll',
        name:'enroll',
        component:Enroll,
        meta:{
          ishendelfooter:true
        }
       },
       {
        path:'/login',
        name:'login',
        component:Login,
        meta:{
          ishendelfooter:true
        }
       },
       {
        path:'/trade',
        name:'trade',
        component:Trade,
        meta:{
          islogin:true
        }
       },
       {
        path:'/notfound',
        name:'notfound',
        component:Notfound,
       },
       {
        path:'/search/:keyword?',
        name:'search',
        component:Search,
       },
       
       {
        path:'/detail/:skuId',
        name:'detail',
        component:Detail,
        props:true
       },
       {
        path:'/comshopcart',
        name:'comshopcart',
        component:Comshopcart,
        props:true
       },
       {
        path:'/shopcart',
        name:'shopcart',
        component:Shopcart,
        props:true
       },
    ],
    scrollBehavior(to,from,scrollpostion){
      if(scrollpostion){
        return scrollpostion
      }else{
        return { x:0,y:0}
      }
    }

})
router.beforeEach((to,from,next) => {
     if(to.meta.islogin){
      if(localStorage.getItem('token')){
        next()
      }else{
        next({path:'/login',query:{url:to.path}})
      }
     }else{
      next()
     }
})